package com.synnex.mobile.servier.core.actRecord.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.actRecord.bo.ActivityRecordBo;
import com.synnex.mobile.servier.core.actRecord.dao.ActRecordDao;
import com.synnex.mobile.servier.core.actRecord.dto.ActivityRecord;
import org.bouncycastle.asn1.isismtt.x509.Restriction;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-5
 * To change this template use File | Settings | File Templates.
 */
public class ActRecordDaoImpl extends GenericHibernateDaoImpl<ActivityRecord, Integer> implements ActRecordDao {

    public List<ActivityRecord> ListActRecord(String key,String category) {
        Criteria c = this.getSession().createCriteria(ActivityRecord.class);

        if(key!=null && !"".equals(key)){
            c.add(Restrictions.like("title",key, MatchMode.ANYWHERE));
        }
        if(category!=null && !"".equals(category)){
            c.add(Restrictions.eq("category", category));
        }
        c.addOrder(Order.asc("seq"));
        return c.list();
    }

    public void addActRecord(ActivityRecord record) {
        super.save(record);
    }

    public void editActRecord(ActivityRecord record) {
        super.update(record);
    }

    public ActivityRecord getActRecordById(Integer id) {
        return super.get(id) ;
    }

    public void deleteActRecordById(Integer id) {
        super.delete(super.get(id));
    }

    public List<Date> getDate(String category) {
        StringBuilder sb = new StringBuilder("Select distinct(ar.activityDay) from ActivityRecord ar ");

        if(category != null && !category.equals("")){
            sb.append(" where ar.category=:cate");
        }
        sb.append(" order by ar.activityDay");

       Query query = this.getSession().createQuery(sb.toString());
        if(category != null && !category.equals("")){
            query.setParameter("cate",category);
        }
        return query.list();
    }
}
